package scenic;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.bson.BSONObject;
import org.bson.BasicBSONObject;

import aliyun.AliYunUpload;
import cn.imnaer.service.persistence.model.City;
import cn.imnaer.service.persistence.model.Recommend;
import cn.imnaer.service.persistence.model.Scenic;
import cn.imnaer.service.rpc.location.CityDaoImpl;
import cn.imnaer.service.rpc.recommend.dao.RecommendDaoImpl;
import cn.imnaer.service.rpc.scenic.dao.ScenicDaoImpl;

public class ScenicParser {

	public static void main(String[] args) throws Exception {
		BSONObject matcherd=new BasicBSONObject();
		ScenicDaoImpl.getInstance().delete(matcherd);
		String path = "/Volumes/我的文件/景点";
		File file = new File(path);
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (File cityFile : files) {
				String cityName = cityFile.getName();
				System.out.println(cityName);
				  if(!cityName .contains(".")){
				BSONObject matcher = new BasicBSONObject();
				matcher.put("cityName", cityName);
				City city = CityDaoImpl.getInstance().findOne(matcher);
				System.out.println(city);
				for (File singleFile : cityFile.listFiles()) {
					String fileName = singleFile.getName();
					if (fileName.contains("docx")) {
						System.out.println(fileName);
						FileInputStream fis = new FileInputStream(singleFile);
						XWPFDocument document = new XWPFDocument(fis);
						List<XWPFPictureData> picList = document.getAllPictures();
						List<String> image = new ArrayList<>();
						for (XWPFPictureData pic : picList) {
							byte[] bytev = pic.getData();
							String fileExtension = pic.suggestFileExtension();
							String contentType = "image/" + fileExtension;
							ByteArrayInputStream input = new ByteArrayInputStream(bytev);
							String key = String.valueOf(System.currentTimeMillis()) + "." + fileExtension;
							AliYunUpload.uploadFile(key, input, bytev.length, contentType);
							input.close();
							image.add(key);
						}
						fis.close();
						// 插入数据库
						if (city != null) {
							String scenicName = fileName.split("\\.")[0];
							Scenic scenic = new Scenic();
							scenic.setScenicName(scenicName);
							scenic.setImages(image);
							scenic.setCity(city);
							scenic.setType("PLAY");
							ScenicDaoImpl.getInstance().insert(scenic, true);
							System.out.println(scenic);
						}
					}
				}
			}
			}
		}
	}

}
